package com.example.qianfeng.adpter;

/**
 * 适配器模式
 * 让两个没有关联的类通过适配器联系起来
 */
public class AdapterMethod {
    public static void main(String[] args) {
        PowerA powerA = new PowerAImpl();
        // work(powerA);

        PowerB powerB = new PowerBImpl();
        AdapterPowerA adapter = new AdapterPowerA(powerB);
        work(adapter);
    }

    /**
     * 装入适配器类
     *
     * @param powerA powerA
     */
    public static void work(PowerA powerA) {
        System.out.println("正在连接...");
        powerA.insert();
        System.out.println("连接成功...");
    }
}

/**
 * A接口
 */
interface PowerA {
    void insert();
}

/**
 * A 接口的实现类
 */
class PowerAImpl implements PowerA {

    @Override
    public void insert() {
        System.out.println("A 插入成功了");
    }
}

/**
 * B 接口
 */
interface PowerB {
    void connect();
}

/**
 * B 接口的实现类
 */
class PowerBImpl implements PowerB {

    @Override
    public void connect() {
        System.out.println("B 插入成功了");
    }
}

/**
 * 创建适配器类
 */
class AdapterPowerA implements PowerA {
    private PowerB powerB;

    public AdapterPowerA(PowerB powerB) {
        this.powerB = powerB;
    }

    @Override
    public void insert() {
        System.out.println("适配器连接成功了..");
    }
}
